package com.travel.server.manager.impl.command.question;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.question.QueryQuestionDetailRequest;
import com.travel.server.manager.dto.response.question.QueryQuestionDetailResponse;
import com.travel.server.manager.dto.response.question.QueryQuestionDetailResponse.QuestionItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.QuestionnaireDal;
import com.travel.server.manager.impl.dal.QuestionnaireDetailDal;
import com.travel.server.manager.impl.dal.QuestionnaireDetailDal.QuestionnaireDetailParam;
import com.travel.server.manager.model.QuestionnaireDetailInfo;
import com.travel.server.manager.model.QuestionnaireInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/16 19:08
 * @Desc:
 */
@Component
@Slf4j
public class QueryQuestionDetailCommand extends AbstractCommand<QueryQuestionDetailRequest, QueryQuestionDetailResponse> {

    @Autowired
    private QuestionnaireDal questionnaireDal;

    @Autowired
    private QuestionnaireDetailDal questionnaireDetailDal;

    @Override
    protected void checkInput(QueryQuestionDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryQuestionDetailResponse handle(QueryQuestionDetailRequest request) {
        checkInput(request);

        QuestionnaireInfo questionnaireInfo = questionnaireDal.selectById(request.getId());

        QuestionnaireDetailParam param = new QuestionnaireDetailParam();
        param.setQuestionnaireId(request.getId());
        List<QuestionnaireDetailInfo> list = questionnaireDetailDal.selectByExample(param);

        return makeResponse(questionnaireInfo, list);
    }

    private QueryQuestionDetailResponse makeResponse(QuestionnaireInfo questionnaireInfo, List<QuestionnaireDetailInfo> list){
        QueryQuestionDetailResponse response = new QueryQuestionDetailResponse();
        List<QuestionItem> questionList = new ArrayList<>();
        response.setBackGroundUrl(questionnaireInfo.getBackGroundUrl());
        response.setDescribe(questionnaireInfo.getQuestionnaireDescribe());
        response.setName(questionnaireInfo.getQuestionnaireName());
        response.setTitle(questionnaireInfo.getQuestionnaireTitle());
        if(!CollectionUtil.isEmpty(list)){
            for(QuestionnaireDetailInfo questionnaireDetailInfo : list){
                QuestionItem item = new QuestionItem();
                item.setId(questionnaireDetailInfo.getId());
                item.setType(questionnaireDetailInfo.getQuestionType());
                item.setAnswerList(questionnaireDetailInfo.getAnswerList());
                item.setQuestion(questionnaireDetailInfo.getQuestion());
                questionList.add(item);
            }
        }
        response.setQuestionList(questionList);
        return response;
    }
}
